<?php
/*
 * Author: Vuong Van Binh
 * Email : wanbin@vietnambiz.com
*/
//MEMBR REGISTER
function add_member() {

    global $CONFIG,$db;
    if(isset($_POST["post_register"])) {
        /*$address = $_POST["address"]." ".get_name_geo(intval($_POST["district"]))." ".get_name_geo(intval($_POST["city"]));
		 	echo  $_POST["company"]."-".$_POST["fullnamecom"]."-".$_POST["emailcom"]."-".$_POST["usernamecom"]."-".$_POST["passwordcom"]."-".$_POST["repasswordcom"]."-".$_POST["codecom"]."-".$_POST["acceptcom"]."-".$_SESSION['captcha_val']."-".$_POST["city"]."-".$_POST["district"]."-".$_POST["phone"].$_POST["address"]."-address:".$address;  die;*/
        $error = false;
        $data = array();
        $data["username"]	 	= strip_tags($_POST["usernamecom"]);
        $data["fullname"] 		= strip_tags($_POST["fullnamecom"]);
        $data["company"] 		= strip_tags($_POST["company"]);
        $data["email"]    		= strip_tags($_POST["emailcom"]);
        $data["password"] 		= md5($_POST["passwordcom"]);
        $data["status"]   		= 'no';
        $data["type"]   		= 'company';
        $data["reg_date"] 		= date('Y-m-d h:i:s',time());
        $data["email"]    		= strip_tags($_POST["emailcom"]);
        $data["confirm_code"] = md5(time());

        if(empty($data["fullname"])) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('fullnamecom')";
            echo "</script>";

        }

        if(empty($_POST["address"])) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('address')";
            echo "</script>";

        }

        if(!valid_email($data["email"])||check_email_exist($data["email"])) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('emailcom')";
            echo "</script>";
            ?>
<script type="text/javascript">
    $("#warning_emailcom").html('<strong>Email này đã được sử dụng</strong><br> Mời bạn chọn email khác.');
</script>
            <?php

        }

        if(check_username_exist($data["username"])||empty($data["username"])) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('usernamecom')";
            echo "</script>";
            ?>
<script type="text/javascript">
    $("#warning_usernamecom").html('<strong>Tên đăng nhập đã tồn tại</strong><br> Mời bạn chọn tên khác.');
</script>
            <?php

        }
        if(strip_tags($_POST["repasswordcom"])!=strip_tags($_POST["passwordcom"]) || empty($_POST["passwordcom"])||strlen(strip_tags($_POST["passwordcom"]))<6 ||strlen(strip_tags($_POST["passwordcom"]))>20) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('repasswordcom')";
            echo "</script>";

        }
        if(strtolower($_POST["codecom"])!=strtolower($_SESSION['captcha_val'])) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('codecom')";
            echo "</script>";

        }
        if($_POST["acceptcom"]!=1) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('acceptcom')";
            echo "</script>";

        }

        if(!is_numeric($_POST["phone"])||empty($_POST["phone"])||strlen($_POST["phone"])<8||strlen($_POST["phone"])>14) {

            $error = true;
            echo "<script type='text/javascript'>";
            echo "display_error('phone')";
            echo "</script>";

        }

        if($error ==false) {

            $insert = $db->exec_insert("members",$data);

            if(mysql_affected_rows()>0) {

                $address = $_POST["address"]." ".get_name_geo(intval($_POST["district"]))." ".get_name_geo(intval($_POST["city"]));

                $insert = $db->exec_insert("members_info",array("mem_id"=>mysql_insert_id(),"phone"=>$_POST["phone"],"address"=>$address));
                //BEGIN SEND MAIL TO MEMBER

                $tempmail_dk = $db->query_first("SELECT subject,content FROM mail_temp WHERE id =1");
                $email   = $data["email"];

                $subject = str_replace("[username]",$data["username"],$tempmail_dk["subject"]);
                $subject = str_replace("[fullname]",$data["fullname"],$subject);

                $link_confirm = SITE_URL."/kich-hoat-tai-khoan/".$data["username"]."/".$data["confirm_code"].".html";

                $content = str_replace("[username]",$data["username"],$tempmail_dk["content"]);
                $content = str_replace("[fullname]",$data["fullname"],$content);
                $content = str_replace("[password]",strip_tags($_POST["passwordcom"]),$content);
                $content = str_replace("[linkconfirm]",$link_confirm,$content);

                ///Get SMPT Server Info and send mail
                $smtp=$db->query_first("SELECT * FROM smtp WHERE 1 ORDER BY RAND() LIMIT 0,1");

                $cfg['smtp_host']=$smtp['host'];
                $cfg['smtp_user']=$smtp['user'];
                $cfg['smtp_pass']=$smtp['pass'];

                $mail = new PHPMailer();
                $mail->IsSMTP();
                $mail->SetLanguage("vn", "");
                $mail->Host     = $cfg['smtp_host'];
                $mail->SMTPAuth = true;
                $mail->Username = $cfg['smtp_user'];
                $mail->Password = $cfg['smtp_pass'];
                $mail->From     = $CONFIG["sender_email"];
                $mail->FromName = $CONFIG["name_sender_email"];
                $mail->AddAddress("$email","$email");
                $mail->AddReplyTo($CONFIG["sender_email"],$CONFIG["name_sender_email"]);
                $mail->IsHTML(true);
                $mail->Subject  =  $subject;

                $mail->Body		=  $content;
                if($mail->Send()) {

                    $CONFIG["success_message"] = "<li>Send Mail Successfully.</li>";

                    $db->query("UPDATE smtp SET `count`=`count`+1 WHERE id=".$smtp['id']);

                    unset($_POST);

                }else {
                    $CONFIG["error_message"] = "<li>Send mail failed!</li>";
                }

                server_redirect(get_link("success").".html");

                unset($_POST);

            }

        }
    }

}
function check_email_exist($email) {

    global $db;

    $mem_rs = $db->query_first("SELECT mem_id FROM members WHERE email = '".mysql_real_escape_string($email)."' ");

    if($mem_rs['mem_id']>0) {

        return true;

    }

    return false;

}
function check_username_exist($username) {

    global $db;

    $mem_rs = $db->query_first("SELECT mem_id FROM members WHERE username = '".mysql_real_escape_string($username)."' ");

    if($mem_rs['mem_id']>0) {

        return true;

    }

    return false;

}
function get_all_geo($parent_id=0) {

    global $db;

    $where = "WHERE geo_parent=".intval($parent_id);

    if($parent_id==0) {
        $where = "WHERE geo_parent=1 OR geo_parent=2 OR geo_parent=3";
    }

    $qr=$db->query("SELECT geo_id,geo_title FROM geographic ".$where." ORDER BY geo_id ASC");

    return $qr;

}
function get_name_geo($id) {

    global $db;

    $geo_rs = $db->query_first("SELECT geo_title FROM geographic WHERE geo_id = ".intval($id));

    if(!empty($geo_rs['geo_title'])) {

        return $geo_rs['geo_title'];

    }

    return "other";

}
?>
